package com.newer.textdbnotepad

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.newer.textdbnotepad.databinding.MainActivityBinding
import com.newer.textdbnotepad.sdk.KeyBuilder
import com.newer.textdbnotepad.sdk.SessionManager
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private val viewModel: MainViewModel by viewModels()

    private val listAdapter = NoteAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = MainActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.tvCreate.setOnClickListener { createNote() }
        with(binding.rvList) {
            layoutManager = LinearLayoutManager(this@MainActivity)
            adapter = listAdapter
        }

        viewModel.listOfNotes.observe(this) { listAdapter.setNotes(it) }

        viewModel.getAllNotes()

    }

    private fun createNote() {
        val newKey = KeyBuilder.genNoteKey(SessionManager.getCurrentUserKey().orEmpty())
        NoteDetailActivity.launch(this, newKey)
    }
}